package com.wnxy.stu.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wnxy.common.entity.Stu;
import com.wnxy.common.result.Result;
import com.wnxy.stu.service.IStuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author yellow docker
 * @since 2024-04-17
 */
@RestController
@RequestMapping("/stu")
public class StuController {

    @Autowired
    private IStuService stuService;

    @Value("${server.port}")
    private Integer port;

    @GetMapping("list")
    public Result list(){
        List<Stu> list = stuService.list();
        return Result.ok(list);
    }

    @GetMapping("list/{id}")
    public Result listById(@PathVariable Integer id){
        Stu stu = stuService.getById(id);
        System.out.println("当前的服务端口 = " + port);
        return Result.ok(stu);
    }

    /**
     * 分页查询 : (配置分页插件)
     *
     *   1、组装分页数据
     *   2、调用page方法
     *   3、拿到page 数据
     *   4、返回result 结果
     *
     * @param currentPage
     * @param pageSize
     * @return
     */
    @GetMapping("list/{currentPage}/{pageSize}")
    public Result listByPages(@PathVariable Integer currentPage,@PathVariable Integer pageSize){
        Page<Stu> p = new Page<Stu>(currentPage, pageSize);
        Page<Stu> page = stuService.page(p);
        return Result.ok(page);
    }

    @PostMapping("add")
    public Result add(@RequestBody Stu stu){
        stuService.save(stu);
        return Result.ok();
    }

    @DeleteMapping ("remove/{id}")
    public Result remove(@PathVariable Integer id){
        stuService.removeById(id);
        return Result.ok();
    }

    @PutMapping("update")
    public Result update(@RequestBody Stu stu){
        stuService.updateById(stu);
        return Result.ok();
    }
}
